package com.sky.controller.user;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api("菜品浏览接口")
@RestController("userDishController")
@RequestMapping("/user/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result getDishByCategoryId(Long categoryId) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = "dish_" + categoryId;
        List<DishVO> dish = (List<DishVO>) valueOperations.get(key);
        if (dish != null && dish.size() > 0) {
            return Result.success(dish);
        }

        Dish build = Dish.builder().status(StatusConstant.ENABLE).categoryId(categoryId).build();
        dish = dishService.getDishByCategoryId(build);

        valueOperations.set(key,dish);

        return Result.success(dish);
    }
}

















